//app.js
Object.assign = Object.assign && typeof Object.assign === 'function' ? Object.assign : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key] } } } return target }
Array.from = Array.from && typeof Array.from === 'function' ? Array.from : obj => [].slice.call(obj)

var that = this

var hostUrl = require("./host.js");
var util = require("./utils/util.js");
var md5Utf_8 = require("./utils/md5Utf_8.js");


import Wux from 'components/wux';
import WxValidate from 'assets/plugins/WxValidate';

App({

  Wux: Wux,
  WxValidate: (rules, messages) => new WxValidate(rules, messages), 

  onLaunch: function () {
    var that = this;
    var utoken = wx.getStorageSync("utoken");
    if (utoken){
      this.userAgainUserInfo();
      console.log("userAgainUserInfo");
    }else{
      this.getUserInfo();
      console.log("getUserInfo");
    }
    this.onMasage();
    this.onContribution_valuation_msg();
  },

  // 获取用户信息
  getUserInfo: function (cb) {
    var that = this
      //调用登录接口
      wx.login({
        success: function (e) {
          console.log("wx.login:success");
          // console.log(e);
          wx.getUserInfo({
            withCredentials: true,
            success: function (res) {
              console.log("wx.getUserInfo:success");
              wx.setStorageSync('islogin', true);
              that.userRequest(e, res);
            }, fail: function () {
              //弹出model框选择
              that.model();
            }
          })
        }
      });
  },

  //如果用户拒绝授权则弹出对话框继续授权
  model: function () {
    var that = this
    wx.showModal({
      title: '警告通知',
      content: '您点击了拒绝授权,将无法正常操作小程序,点击确定重新获取授权。',
      success: function (res) {
        if (res.confirm) {
          wx.openSetting({
            success: (res) => {
              if (res.authSetting["scope.userInfo"]) {////如果用户重新同意了授权登录
                wx.login({
                  success: function (e) {
                    if (e.code) {
                      wx.getUserInfo({
                        withCredentials: true,
                        success: function (res) {
                          //请求后台服务器
                          that.userRequest(e, res);
                        }
                      })
                    }
                  }
                });
              }
            },
          })
        }
      }
    })
  },
  userLogin: function () {

  },

  paramsMd5: function (data) {
    return util.md5Param(data);
  },

  //请求后台服务器
  userRequest: function (e, res) {
    console.log("userRequest:function");
    wx.setStorageSync('userInfo', res.rawData);
    wx.setStorageSync('avatarUrl', res.rawData.avatarUrl);
    var url = hostUrl.productInfo;
    var data = {
      code: e.code,
      iv: res.iv,
      encryptedData: res.encryptedData
    };
    var method = 'POST';
    var params = util.md5Param(data);
    var that = this;
    //请求服务器
    wx.request({
      url: url,
      data: params,
      method: method,
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      dataType: 'json',
      success: function (response) {
        if (response.data.status) {
          that.globalData.utoken = response.data.msg.utoken;
          wx.setStorageSync("utoken",response.data.msg.utoken);
          that.globalData.have_calculation = response.data.msg.have_calculation;
          console.log(response.data.msg)
        }else{

        }
      }
    });
  },

  //用户再次进入系统
  userAgainUserInfo:function(){
    var url = hostUrl.again_user_info;
    var data = {
      utoken:wx.getStorageSync("utoken"),
    };
    var method = 'POST';
    var params = util.md5Param(data);

    var that = this;
    //请求服务器
    wx.request({
      url: url,
      data: params,
      method: method,
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      dataType: 'json',
      success: function (response) {

        if (response.data.status) {
          
          wx.setStorageSync("utoken", response.data.msg.utoken);
          that.globalData.have_calculation = response.data.msg.have_calculation;
          console.log(response.data.msg)
        }else{

          that.getUserInfo();
        }
      }
    });
  },

  onMasage: function () {
    //请求文字描述接口并缓存数据
    var url = hostUrl.shareholders_score_msg;
    var paramsMd5 = this.paramsMd5({ utoken: wx.getStorageSync("utoken") });
    var that = this;
    wx.request({
      url: url,
      data: paramsMd5,
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      },
      dataType: 'json',
      success: function (response) {
        console.log("shareholders_score")
        // console.log(response);
        if (response.data.status) {
          that.globalData.shareholders_score = response.data.msg;
        }
      },
    });
  },

  onContribution_valuation_msg: function () {
    //请求文字描述接口并缓存数据
    var url = hostUrl.contribution_valuation;
    var paramsMd5 = this.paramsMd5({ utoken: wx.getStorageSync("utoken") });
    var that = this;
    wx.request({
      url: url,
      data: paramsMd5,
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      dataType: 'json',
      success: function (response) {
        // console.log(response)
        if (response.data.status) {
          that.globalData.contribution_valuation = response.data.msg;
        }
      }
    });
  },
  globalData: {
    have_calculation : null,
    userInfo: null,//用户信息
    utoken: "",
    nickName: null,
    islogin: false,//是否登录
    shareholders_score:{},  //股东贡献分配法循环页面msg
    contribution_valuation: {},//-股东贡献循环页面msg
    //用来做页面循环跳转
    entity: {},   //info页面数据准备
    shareholders_list: [],  //股东数量
    contribution_score_weight_list: {},//权重页面贡献比重
    shareholders_contribution_valuation_list: {},
    shareholders_score_list: {},
    currentBoss: 0, //贡献评分当前页
    shareDistribution: 0,  //shareDistribution当前页
    bossName: "", //当前股东股东名称
    record_id : "",
  },

  //清除缓存数据
  cleraGlobalData: function () {
    var  that = this;
    that.globalData.entity = {};
    that.globalData.shareholders_list = [];
    that.globalData.shareholders_score_list = {};
    that.globalData.contribution_score_weight_list = {};//权重页面贡献比重
    that.globalData.shareholders_contribution_valuation_list = {};
    that.globalData.currentBoss = 0; //贡献评分当前页
    that.globalData.shareDistribution = 0; //shareDistribution当前页
    that.globalData.record_id ="";
  }
})